package com.tarena.lbs.business.servcie.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.pojo.vo.StoreChainVO;
import com.tarena.lbs.business.servcie.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessRepository businessRepository;

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public void save(BusinessParam param) {
        log.debug("保存商家信息业务，入参:{}", param);
        BusinessPO po = new BusinessPO();
        BeanUtils.copyProperties(param, po);
        po.setEntryTime(new Date());
        businessRepository.save(po);
    }

    @Override
    public PageResult<BusinessVO> pageList(BusinessQuery query) {
        //创建一个新的分页结果对象
        PageResult<BusinessVO> voPage = new PageResult<>(query);
        //total objects pageNo pageSize totalPage
        //调用 businessRepository 的 gePages 方法进行分页查询，获取包含商家持久化对象的分页信息
        PageInfo<BusinessPO> pageInfo = businessRepository.pagesList(query);
        //pageNo pageSize total
        // 将分页信息中的页码设置到分页结果对象中
        voPage.setPageNo(pageInfo.getPageNum());
        // 将分页信息中的每页数量设置到分页结果对象中
        voPage.setPageSize(pageInfo.getPageSize());
        // 将分页信息中的总记录数设置到分页结果对象中
        voPage.setTotal(pageInfo.getTotal());
        // 声明一个存储商家视图对象的列表
        List<BusinessVO> vos = null;
        // 从分页信息中获取管理员持久化对象列表
        List<BusinessPO> pos = pageInfo.getList();
        // 判断持久化对象列表是否不为空
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                BusinessVO vo = new BusinessVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }


    @Override
    public BusinessStoreVO busiStoreDetail(Integer businessId) {
        //查询商家信息
        BusinessStoreVO vo=new BusinessStoreVO();
        BusinessVO businessVO = businessMapper.busiStoreDetail(businessId);
        BeanUtils.copyProperties(businessVO, vo);
        //查询商家下的店铺信息
        vo.setStoreVOList(businessMapper.storeDetail(businessId));
        return vo;
    }

    @Override
    public List<StoreChainVO> getAll() {

        return  businessRepository.getAll();
    }

    @Override
    public int updateStatus(Integer id) {
        return businessRepository.updateStatus(id);
    }
}
